﻿// =============================================================================
// TNTTools SerializerFacade.cs
// Copyright (c) 2013 TNTTechnologies Inc.
// =============================================================================

#region

using System;
using System.IO;
using System.Text;

using TNTTools.Serialization;

#endregion

namespace TNTTools{
    public class SerializerFacade{
        public static StringBuilder SerializeBasic(object source){
            StringBuilder builder = new StringBuilder();
            SimpleSerialization serializer = new SimpleSerialization(new ReflectionPropertyCache());
            serializer.Serialize(builder, source);
            return builder;
        }

        /// <summary>
        ///     Serialize with support for attributes such as [XMLIgnore]
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static StringBuilder SerializeWithAttributeInformation(object source){
            StringBuilder builder = new StringBuilder();
            AttributeInfoMarkedSerializer serializer =
                new AttributeInfoMarkedSerializer(new AttributeInfoReflectionPropertyCache());
            serializer.Serialize(builder, source);
            return builder;
        }

        /// <summary>
        ///     Serialize with support for attributes such as [XMLIgnore]
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static Stream SerializeWithAttributeInformation(object source, Stream stream){
            StringBuilder builder = new StringBuilder();
            AttributeInfoMarkedSerializer serializer =
                new AttributeInfoMarkedSerializer(new AttributeInfoReflectionPropertyCache());
            serializer.Serialize(stream, source);
            return stream;
        }

        /// <summary>
        ///     Serialize raw
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static Stream Serialize(object source, Stream stream){
            SimpleSerialization serializer = new SimpleSerialization(new ReflectionPropertyCache());
            serializer.Serialize(stream, source);
            return stream;
        }

        /// <summary>
        ///     Deserialize stream into an object
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static T Deserialize<T>(Stream stream){
            StringBuilder builder = new StringBuilder();
            SimpleSerialization serializer = new SimpleSerialization(new ReflectionPropertyCache());
            return (T)serializer.Deserialize(stream, typeof(T));
        }

        /// <summary>
        ///     Deserialize string
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static T Deserialize<T>(String XML){
            // convert string to stream
            MemoryStream stream = new MemoryStream();
            StreamWriter writer = new StreamWriter(stream);
            writer.Write(XML);
            writer.Flush();

            stream.Position = 0;

            SimpleSerialization serializer = new SimpleSerialization(new ReflectionPropertyCache());
            return (T)serializer.Deserialize(stream, typeof(T));
        }
    }
}